#!/usr/bin/env python3

import logging
import shutil
import pathlib

target_dir = r''

my_logger = logging.getLogger(__name__)


def delete_whatever_the_fuck_this_is(path: pathlib.Path) -> None:
    """
    垃圾Python，没有一个像`rm -rf`一样能删除所有东西的函数，
    害的老子手写了一个，为了写这个东西还多导入了一个`shutil`库
    :param path: 要删除的路径
    :return: 无
    """
    if path.is_dir():
        shutil.rmtree(path.absolute())
    else:
        path.unlink()
    my_logger.warning(f'{path.name} has been deleted')


def to_bare(git_repo_root_path: pathlib.Path) -> None:
    dot_git_folder_path = git_repo_root_path / '.git'
    path_list = git_repo_root_path.glob('*')
    for path in path_list:
        if path != dot_git_folder_path:
            delete_whatever_the_fuck_this_is(path)


def search_dir(current_dir: str) -> None:
    current_path = pathlib.Path(current_dir)
    dot_git_folder_path = current_path / '.git'
    if dot_git_folder_path.is_dir():
        my_logger.info(f'Git repo found: {current_dir} !')
        to_bare(current_path)
    else:
        for path in current_path.glob('*'):
            if path.is_dir():
                search_dir(path)


def set_logger(my_logger):
    my_logger.setLevel(logging.INFO)
    
    file_handler = logging.FileHandler('my_logger.log', mode='w')
    stream_handler = logging.StreamHandler()
    
    my_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    
    file_handler.setFormatter(my_formatter)
    stream_handler.setFormatter(my_formatter)
    
    my_logger.addHandler(file_handler)
    my_logger.addHandler(stream_handler)


if __name__ == '__main__':
    set_logger(my_logger)
    my_logger.info('start')
    search_dir(target_dir)
